Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Code in the procedure can set KeepFocus to True to cause the control to keep the focus after the event procedure executes. Leaving KeepFocus at the default value of False permits the focus to move away from the control. Here’s an example Validate procedure that ensures that the data entered in the Text Box is a number:
    Private Sub Text1_Validate(KeepFocus As Boolean)

    If Not IsNumeric(Text1.Text) Then
        KeepFocus = True
        MsgBox “You must enter a number here.”
    End If

    End Sub
What sort of things do you look for when validating data? It depends on the specific situation, of course, but several general criteria are often used: • Is a number within a certain range? For example, a Quantity on Hand field may be zero or positive, but never negative. • Is a text entry the proper length or within the acceptable length range? For example, a ZIP code (entered as text) must be 5 characters long (or 10 characters for “ZIP + 4”). • Does a text entry fit the required format? Your firm’s Stock Numbers may all follow the format XXXNNNN, where X is a letter and N is a number; any other format of entry should be rejected. • Is a Date entry within the allowable time span? For example, if an employee’s birth date is entered as May 4, 1852, you can be reasonably sure a mistake was made. The Basic language provides a full range of tools for validating data. Use of the Variant data type in data-validation routines is recommended. The automatic “awareness” of data type that Variant variables provide can eliminate some tedious coding, particularly when converting strings to numbers and numbers to strings. Functions such as IsNumeric and IsDate make certain types of validation trivial, while Visual Basic’s various string functions, such as Len and Instr, can be used to validate string data. ### Fundamental Program Structure Before starting on a project of this complexity, you should always consider the various options that Visual Basic provides for the overall program structure and appearance. You know that the program requires several “forms” to display the database data in various ways and to permit entry and editing of data (why forms is in quotes will become clear). The user also needs a way to move between forms, selecting the task to be performed. First, look at the available options for displaying program elements: • Use the multiple document interface (MDI) architecture. In an MDI program, one “master” form (the MDI form) serves as a parent or container for all other forms, or child forms. Any number of child forms can exist; and while multiple child forms can be displayed at one time, only one can be active. Display of child forms is limited to the internal area of the parent form: If the parent form does not occupy the full screen, the child forms cannot be moved outside of it. Each child form can have its own menu; when the form is active, this menu is displayed on the menu bar of the parent form. If you have used a Windows word processor, such as Microsoft Word, you have seen an MDI program in action. • Use a collection of non-MDI forms. This configuration also uses multiple forms, but with no parent and no children. Each form is an independent entity that displays its own menu and can be moved and sized independently of the other forms. • Use a single form with multiple Picture Box forms. Remember that a Picture Box control can be used as a container for other controls. In this design, the program consists of a single Visual Basic form with several Picture Box controls on it. Each Picture Box contains the controls required for a particular program function—one for entry of records in the Customers table, another for creating new Invoice records, and so on. Code in the program determines which of the Picture Box controls is visible and active at a given time, permitting the display of multiple “forms” on a single form. Several options are also available for user control of the program. Of course, you can use these different control methods in combination with each other, but for the sake of simplicity, your demonstration project is limited to only one method. In a real, commercial application, you’ll probably want to use two—for example, a Toolbar plus menus. Here are the main ways that you can design a program to accept commands from the user: • Create a menu system containing the commands • Display Command Buttons for the commands • Design a Toolbar with buttons for the commands For the purposes of the demonstration program, I decided to use an MDI interface with a Toolbar. Some other choices might have worked equally well, but this approach gives you the opportunity to learn how to implement an MDI interface and a Toolbar—two important Visual Basic tools. ### Preliminary Form Design Rather than trying to tackle the entire project at once, you start by designing and coding the simpler forms, and creating the MDI interface. For both the Customers table and the Wines table, you need a form that permits entering new records, editing existing records, and deleting existing records. These forms should be fairly straightforward, requiring a Text Box and identifying Label for each field, and a group of Command Buttons for the various actions. You’ll also want a way to list all the records in the table on a single form, with the option of sorting the list based on different fields. In this situation, the DataGrid custom control comes in handy. But first you need the parent MDI form and its Toolbar. #### Creating The MDI Form And Toolbar Creating the MDI form first is not necessary—but it isn’t a bad idea, either. Start a new standard EXE project and select Add MDI Form from the Project menu. Change the form’s Caption property to “Grapevine Wine Distributors”. You can leave the form’s Name property at the default value of MDIForm1, but when you save it, use the name GRAPEVINE.FRM (Use GRAPEVINE as the project name, too). Click-and-drag the Toolbar icon in the Toolbox to place a Toolbar on the form. Remember, if the Toolbar icon is not displayed in the Toolbox, you must use the Project, Components command to load it. The Toolbar is part of the set referred to as the Windows Common Controls. When you select this item in the Components dialog box, several controls, including the Toolbar, will be added to the Visual Basic toolbox. #### Working With The Toolbar Control The Toolbar starts out empty, with no buttons. You have the option of using text or images on the Toolbar buttons. For images, bind the Toolbar to an ImageList control, which manages the images (icons or bitmaps) that are displayed on the Toolbar buttons. For the present project, just use text buttons. I presented information on using the ImageList control in Chapter 7. Before you start to work on the Toolbar buttons, set the Align property of the Toolbar to Top, causing the Toolbar to position itself automatically at the top of the container form, filling it from side to side. This property must be set in the regular Visual Basic properties window—not in the Toolbar’s pop-up property sheet, which you use next. The Toolbar is an object with its own properties, and it contains several Button objects with their own properties. When a Toolbar control is selected on the Visual Basic design screen, the Properties window displays the Toolbar’s properties, which control variables such as the button size and font. To access Button properties and add or delete buttons, you need to display the Toolbar’s property sheet by right-clicking on the control and selecting Properties from the menu that is displayed. The Toolbar Control properties page—another name for dialog box—is shown in Figure 22.4.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.